#include <bits/stdc++.h>

using namespace std;

int n,a[30],mp[30][30],path[30],ans[30],maxn;
void dfs(int cur,int sum,int step){
    if(maxn<sum){
        maxn = sum;
        for(int i=1;i<=n;i++){
            ans[i]=path[i];
        }
    }
    for(int i=1;i<=n;i++){
        if(mp[cur][i]){
            path[step] = i;
            dfs(i,sum+a[i],step+1);
            path[step] = 0;
        }
    }
}
int main()
{
    cin>>n;
    for(int i=1;i<=n;i++){
        cin>>a[i];
    }
    for(int i=1;i<n;i++){
        for(int j=i+1;j<=n;j++){
            cin>>mp[i][j];
        }
    }

    for(int i=1;i<=n;i++){
        path[1] = i;
        dfs(i,0+a[i],2);
        path[1] = 0;
    }
    for(int i=1;i<=n;i++){
        if(ans[i]>0)
        cout<<ans[i]<<' ';
    }
    cout<<endl<<maxn;
    return 0;
}
